package gd.qaq.blue.powerparticle.listener;

import gd.qaq.blue.powerparticle.PowerParticle;
import gd.qaq.blue.powerparticle.utils.Particles;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;

import static gd.qaq.blue.powerparticle.PowerParticle.config;

public class PowerParticleListener implements Listener {

    public PowerParticleListener(PowerParticle powerParticle) {
    }

    @EventHandler
    public void onMove(PlayerMoveEvent event) {
        Player player = event.getPlayer();
        if (player != null && !Particles.playersParticleTask.containsKey(player.getUniqueId().toString())) {
            double maxHealth = player.getMaxHealth();
            if (maxHealth >= config.getDouble("plugin.particleLevel.5.health")) {
                getConfigRGB2Spawn("plugin.particleLevel.5",player);
            } else if (maxHealth >= config.getDouble("plugin.particleLevel.4.health")) {
                getConfigRGB2Spawn("plugin.particleLevel.4",player);
            } else if (maxHealth >= config.getDouble("plugin.particleLevel.3.health")) {
                getConfigRGB2Spawn("plugin.particleLevel.3",player);
            } else if (maxHealth >= config.getDouble("plugin.particleLevel.2.health")) {
                getConfigRGB2Spawn("plugin.particleLevel.2",player);
            } else if (maxHealth >= config.getDouble("plugin.particleLevel.1.health")) {
                getConfigRGB2Spawn("plugin.particleLevel.1",player);
            } else {
                Particles.stopParticle(player);
            }
        }
    }

    private void getConfigRGB2Spawn(String path, Player player) {
        float r = (float) config.getDouble(path + ".red");
        float g = (float) config.getDouble(path + ".green");
        float b = (float) config.getDouble(path + ".blue");
        float health = (float) config.getDouble(path + ".health");
        Particles.spawnHelix(player, r <= 0 ? Float.MIN_NORMAL : r, g <= 0 ? Float.MIN_NORMAL : g, b <= 0 ? Float.MIN_NORMAL : b, health);
    }
}
